﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlexControls;
namespace KHOALUAN
{
    public partial class frmHocKy : Form
    {
        
       
        private const string TABLE_NAME = "HOCKY";
        clsMain.XMLData HK = new clsMain.XMLData();
        private bool booDoubleClick = false;
        public frmHocKy()
        {
            InitializeComponent();
        }

        private void frmHocKy_Load(object sender, EventArgs e)
        {
            clsMain.cls.clsForm.intSoForm++;
            try
            {
                
                clsMain.KhoiTaoXMLTable(TABLE_NAME, ref HK, 3);
                HK.ColumnName[0] = "MÃ SỐ HỌC KỲ";
                HK.ColumnName[1] = "TÊN HỌC KỲ";
                HK.ColumnName[2] = "GHI CHÚ";

                HK.Names[0] = "MSHK";
                HK.Names[1] = "TENHK";
                HK.Names[2] = "GHICHU";
                clsMain.AddDataToDataGridView(TABLE_NAME, ref dgvHocKy, HK);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Lỗi từ: " +sender.ToString() +"\nSự kiện: "+ e.ToString()+ "\n\nChi tiết:\n"+ex.Message,"Thông báo lỗi");
            }
        }

        private void butNew_Click(object sender, EventArgs e)
        {
            try
            {
                clsMain.InsertElement(this, TABLE_NAME, HK);
                clsMain.ClearControls(this, HK);
                clsMain.AddDataToDataGridView(TABLE_NAME, ref dgvHocKy, HK);   
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi từ: " + sender.ToString() + "\nSự kiện: " + e.ToString() + "\n\nChi tiết:\n" + ex.Message, "Thông báo lỗi");
            }
            
        }

        private void dgvHocKy_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            butXoa.Enabled = true;
            butUpdate.Enabled = true;
            clsMain.AddDataToControls(this, dgvHocKy, HK);
            booDoubleClick = true;
        }

        private void butXoa_Click(object sender, EventArgs e)
        {
            if (booDoubleClick == true)
            {
                clsMain.DeleteData(TABLE_NAME, HK, dgvHocKy.SelectedCells[0].Value.ToString());
                clsMain.AddDataToDataGridView(TABLE_NAME, ref dgvHocKy, HK);
                booDoubleClick = false;
                clsMain.ClearControls(this, HK);
                butXoa.Enabled = false;
            }
        }

        private void butUpdate_Click(object sender, EventArgs e)
        {
            if (booDoubleClick == true)
            {
                clsMain.UpdateData(this,TABLE_NAME, HK, dgvHocKy.SelectedCells[0].Value.ToString());
                clsMain.AddDataToDataGridView(TABLE_NAME, ref dgvHocKy, HK);
                booDoubleClick = false;
                clsMain.ClearControls(this, HK);
                butXoa.Enabled = false;
                butUpdate.Enabled = false;
            }
        }
        
    }
}
